package antempire.control.game.operation.sceneoperation;

import java.io.IOException;

import org.anddev.andengine.audio.music.Music;
import org.anddev.andengine.audio.music.MusicFactory;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.sprite.TiledSprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.Debug;
import org.anddev.andengine.util.HorizontalAlign;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import antempire.control.GameSound;
import antempire.control.game.AntEmpire;
import antempire.control.game.GameConstants;
import antempire.control.menu.MainMenu;
import antempire.control.menu.R;
import antempire.control.record.MyDataBaseAdapter;

public class SceneConverter implements GameConstants {

	private Sprite returnButton, retryButton, nextButton;
	private TiledSprite soundButton;
	private static int mMusicVolume, mSoundVolume;
	private static Music mBgMusic;
	boolean isEstablished = false;

	public SceneConverter(TextureRegion returnButtonTextureRegion,
			TextureRegion retryButtonTextureRegion,
			TextureRegion nextTextureRegion,
			TiledTextureRegion soundButtonTextureRegion, AntEmpire activity,
			Context context, GameSound mSound) {
		nextButton = this
				.createNextButton(activity, context, nextTextureRegion);
		returnButton = this.createReturnButton(activity, context,
				returnButtonTextureRegion);
		returnButton.setScale(2.0f);
		retryButton = this.createRetryButton(activity, context,
				retryButtonTextureRegion);
		retryButton.setScale(2.0f);
		soundButton = this.createSoundButton(activity,
				soundButtonTextureRegion, mBgMusic, mSound, mSoundVolume);
		mBgMusic.play();
	}

	public static GameSound initialGameSound(BaseGameActivity activity,
			int mMusicVolume, int mSoundVolume) {

		MusicFactory.setAssetBasePath("mfx/");

		try {
			mBgMusic = MusicFactory.createMusicFromAsset(activity.getEngine()
					.getMusicManager(), activity, "BG.ogg");
			mBgMusic.setLooping(true);
		} catch (final IOException e) {
			Debug.e(e);
		}
		SceneConverter.mBgMusic.setVolume(mMusicVolume);
		SceneConverter.mMusicVolume = mMusicVolume;
		SceneConverter.mSoundVolume = mSoundVolume;

		GameSound mSound = new GameSound();
		mSound.create(activity);
		mSound.setVolume(mSoundVolume);

		return mSound;
	}

	public TiledSprite createSoundButton(BaseGameActivity activity,
			TiledTextureRegion soundButtonTextureRegion, final Music mBgMusic,
			final GameSound mSound, final int mSoundVolume) {
		TiledSprite soundButton = new TiledSprite(0, 0,
				soundButtonTextureRegion) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if (mBgMusic.isPlaying()) {
					if (pSceneTouchEvent.isActionUp()) {
						this.setCurrentTileIndex(1);
						mBgMusic.pause();
						mSound.setVolume(0);
					}
				} else {
					if (pSceneTouchEvent.isActionUp()) {
						this.setCurrentTileIndex(0);
						mBgMusic.resume();
						mSound.setVolume(mSoundVolume);
					}
				}
				return true;
			}
		};
		soundButton.setPosition((CAMERA_WIDTH - soundButton.getWidth()) / 2,
				(CAMERA_HEIGHT - soundButton.getHeight()) / 2);
		return soundButton;
	}

	public Sprite createRetryButton(final BaseGameActivity activity,
			final Context context, TextureRegion retryButtonTextureRegion) {

		Sprite retryButton = new Sprite(CAMERA_WIDTH / 2 + 150,
				CAMERA_HEIGHT / 2 + 40, retryButtonTextureRegion) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				Intent intent = new Intent();
				intent.putExtra("musicVolume", mMusicVolume);
				intent.putExtra("soundVolume", mSoundVolume);
				intent.setClass(context, AntEmpire.class);
				activity.startActivity(intent);
				activity.finish();
				return true;
			}
		};
		return retryButton;

	}

	public Sprite createReturnButton(final BaseGameActivity activity,
			final Context context, TextureRegion returnButtonTextureRegion) {

		Sprite returnButton = new Sprite(CAMERA_WIDTH / 2 - 200,
				CAMERA_HEIGHT / 2 + 40, returnButtonTextureRegion) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				Intent intent = new Intent();
				/*
				 * intent.putExtra("musicVolume", mMusicVolume);
				 * intent.putExtra("soundVolume", mSoundVolume);
				 */
				intent.setClass(context, MainMenu.class);
				activity.startActivity(intent);
				activity.finish();
				return true;
			}
		};
		return returnButton;

	}

	public Sprite createNextButton(final AntEmpire activity,
			final Context context, TextureRegion nextTextureRegion) {

		Sprite nextButton = new Sprite(CAMERA_WIDTH * 0.5f + 130,
				CAMERA_HEIGHT * 0.6f + 40, nextTextureRegion) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {

				if (!isEstablished) {
					LayoutInflater mInflater = LayoutInflater.from(context);
					final View view = mInflater.inflate(R.layout.newrecord,
							null);
					AlertDialog.Builder dialog = new AlertDialog.Builder(
							context);

					dialog.setIcon(R.drawable.ic_launcher);
					dialog.setView(view);
					dialog.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									// unimplemented method
									EditText editText = (EditText) view
											.findViewById(R.id.edittext);
									if (editText.getText().toString()
											.equals("")) {
										Toast.makeText(activity,
												"Username is empty，input again",
												Toast.LENGTH_SHORT).show();
										dialog.cancel();
									} else {
										String name = editText.getText()
												.toString();
										// plug in Database and open it for more
										// operations

										MyDataBaseAdapter mDataBase = activity
												.get_DataBase();
										mDataBase.insertData(name, String
												.valueOf(activity.get_Score()));

										Toast.makeText(activity, "Thanks",
												Toast.LENGTH_SHORT).show();
										dialog.dismiss();
										// mDataBase.close();
									}
								}
							});

					dialog.setNegativeButton("CANCEL",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									// unimplemented method
									dialog.dismiss();
								}
							});
					dialog.create().show();// show the dialog
					isEstablished = true;
				}
				return true;
			}
		};

		return nextButton;

	}

	public void onGameEnd(int mScore, Font mScoreFont, AntEmpire activity,
			boolean mGameRunning) {

		Text mScoreText = this.createText(mScoreFont, "SCORE: " + mScore, -30);
		Text mRecordText = this.createText(mScoreFont, "CONGRATULATIONS", -100);
		
		mBgMusic.stop();

		mGameRunning = false;
		int Max = 0;
		MyDataBaseAdapter mDataBase = activity.get_DataBase();
		Cursor cursor = mDataBase.fetchAllData();
		int row = cursor.getCount(), temp;
		if (row != 0) {
			cursor.moveToFirst();
			Max = Integer.valueOf(cursor.getString(2)).intValue();
			if (cursor != null) {
				while (cursor.moveToNext()) {
					temp = Integer.valueOf(cursor.getString(2)).intValue();
					if (temp > Max)
						Max = temp;
				}
			}
		}

		//if (mScore >= Max) {
			if (!isEstablished) {
				Scene recordScene = this.createNewScene(mRecordText,
						mScoreText);
				Text inputText = new Text(0, 0, mScoreFont, "INPUT YOUR USERNAME:",
						HorizontalAlign.CENTER);
				inputText.setPosition(
						(CAMERA_WIDTH - inputText.getWidth()) * 0.5f - 60,
						(CAMERA_HEIGHT - inputText.getHeight()) * 0.6f + 80);
				recordScene.attachChild(inputText);
				recordScene.attachChild(nextButton);
				recordScene.registerTouchArea(nextButton);
				recordScene.attachChild(returnButton);
				recordScene.attachChild(retryButton);
				recordScene.registerTouchArea(returnButton);
				recordScene.registerTouchArea(retryButton);
				activity.getEngine().getScene()
						.setChildScene(recordScene, false, true, true);
			} else {
				activity.getEngine().getScene().clearChildScene();
				Scene newScene = creaNewSceneWithButton(returnButton,
						retryButton);
				activity.getEngine().getScene()
						.setChildScene(newScene, false, true, true);
			}
	}

	private Scene creaNewSceneWhileAddButton(Text text, Text mScoreText,
			Sprite returnButton, Sprite retryButton) {
		Scene newScene = this.createNewScene(text, mScoreText);
		newScene.attachChild(returnButton);
		newScene.attachChild(retryButton);
		newScene.registerTouchArea(returnButton);
		newScene.registerTouchArea(retryButton);

		return newScene;
	}

	private Scene creaNewSceneWithButton(Sprite returnButton, Sprite retryButton) {
		Scene newScene = new Scene(1);
		newScene.setBackgroundEnabled(false);
		newScene.attachChild(returnButton);
		newScene.attachChild(retryButton);
		newScene.registerTouchArea(returnButton);
		newScene.registerTouchArea(retryButton);

		return newScene;
	}

	private Scene createNewScene(Text text, Text mScoreText) {
		Scene newScene = new Scene(1);
		newScene.setBackgroundEnabled(false);
		newScene.attachChild(text);
		newScene.attachChild(mScoreText);
		return newScene;
	}

	private Text createText(Font mFont, String string, int i) {
		Text text = new Text(0, 0, mFont, string, HorizontalAlign.CENTER);
		text.setPosition((CAMERA_WIDTH - text.getWidth()) * 0.5f,
				(CAMERA_HEIGHT - text.getHeight()) * 0.5f + i);
		text.registerEntityModifier(new ScaleModifier(2, 0.1f, 2.0f));
		text.registerEntityModifier(new RotationModifier(2, 0, 720));
		return text;
	}

	public void onPauseGame(BaseGameActivity activity, Scene mPauseScene,
			TiledSprite pauseButton) {

		returnButton.setPosition(CAMERA_WIDTH / 2 + 50,
				(CAMERA_HEIGHT - returnButton.getHeight()) / 2);
		retryButton.setPosition(CAMERA_WIDTH / 2 - retryButton.getWidth() - 50,
				(CAMERA_HEIGHT - retryButton.getHeight()) / 2);
		mPauseScene.attachChild(returnButton);
		mPauseScene.attachChild(retryButton);
		mPauseScene.registerTouchArea(returnButton);
		mPauseScene.registerTouchArea(retryButton);
		mPauseScene.attachChild(pauseButton);
		mPauseScene.registerTouchArea(pauseButton);
		mPauseScene.attachChild(soundButton);
		mPauseScene.registerTouchArea(soundButton);
		
		mBgMusic.pause();

		activity.getEngine().getScene()
				.setChildScene(mPauseScene, false, true, true);
	}

	public void onResumeGame(BaseGameActivity activity) {

		activity.getEngine().getScene().clearChildScene();
		
		mBgMusic.play();

		returnButton
				.setPosition(CAMERA_WIDTH / 2 - 150, CAMERA_HEIGHT / 2 + 40);
		retryButton.setPosition(CAMERA_WIDTH / 2 + 25, CAMERA_HEIGHT / 2 + 40);
	}

}
